package cn.orange.ch07_binarytree;

import java.util.LinkedList;

/**
 * LC116.填充每个节点的下一个右侧节点指针
 */
public class LC116 {
    public Node connect(Node root) {
        if (root == null) {
            return null;
        }
        LinkedList<Node> que = new LinkedList<>();
        que.offer(root);
        while (!que.isEmpty()) {
            int size = que.size();
            Node prev = null;
            for (int i = 0; i < size; i++) {
                Node poll = que.poll();
                if (prev != null) {
                    prev.next = poll;
                }
                if (poll.left != null) {
                    que.offer(poll.left);
                }
                if (poll.right != null) {
                    que.offer(poll.right);
                }
                prev = poll;
            }
        }
        return root;
    }
    public static void main(String[] args) {
        LC116 alg = new LC116();
        
    }
}
